package com.sskj.ffex.v.activity;

import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.CheckedTextView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sskj.ffex.MyAppLication;
import com.sskj.ffex.R;
import com.sskj.ffex.common.App;
import com.sskj.ffex.common.Constants;
import com.sskj.ffex.common.widget.ToolBarLayout;
import com.sskj.ffex.m.bean.AppVersionBean;
import com.sskj.ffex.p.activity.GoogleSettingPresenter;
import com.sskj.ffex.utils.APKVersionCodeUtils;
import com.sskj.ffex.utils.ClickUtil;
import com.sskj.ffex.utils.LanguageUtil;
import com.sskj.ffex.utils.SPUtils;
import com.sskj.ffex.utils.TipUtil;
import com.sskj.ffex.utils.ToastUtil;
import com.sskj.ffex.utils.fingerprint.FingerprintCore;
import com.sskj.ffex.utils.fingerprint.FingerprintUtil;
import com.sskj.ffex.v.base.BaseActivity;
import com.sskj.ffex.v.fragment.AppUpDateFragment;

import java.util.Locale;

import butterknife.BindView;
import sskj.lee.appupdatelibrary.BaseUpdateDialogFragment;
import sskj.lee.appupdatelibrary.BaseVersion;
import zwh.com.lib.RxFingerPrinter;

public class GoogleSettingActivity extends BaseActivity<GoogleSettingPresenter> {

    @BindView(R.id.item_switch)
    CheckedTextView itemSwitch;
    @BindView(R.id.language_choose)
    TextView languageChoose;  // 语言选择
    @BindView(R.id.zw_switch)
    CheckedTextView mineSettingZw;  // 指纹验证开关
    @BindView(R.id.version)
    TextView version;
    @BindView(R.id.toolbar)
    ToolBarLayout toolBarLayout;
    @BindView(R.id.version_layout)
    LinearLayout versionLayout;

    private int type;
    private RxFingerPrinter mRxFingerPrinter;
    private FingerprintCore mFingerprintCore;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_google_setting;
    }

    @Override
    public GoogleSettingPresenter getPresenter() {
        return new GoogleSettingPresenter();
    }

    public static void start(Context context, int type) {
        Intent intent = new Intent(context, GoogleSettingActivity.class);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    @Override
    protected void onViewBinding() {
        super.onViewBinding();
        mFingerprintCore = new FingerprintCore(this);
        mRxFingerPrinter = new RxFingerPrinter(this);
        if (!mFingerprintCore.isSupport()) mineSettingZw.setVisibility(View.GONE);
        if ("en".equals(MyAppLication.getLANGUAGE())) {
            languageChoose.setText(App.INSTANCE.getString(R.string.strGoogleSettingActivity207));
        } else {
            languageChoose.setText(App.INSTANCE.getString(R.string.strGoogleSettingActivity205));
        }

        type = getIntent().getIntExtra("type", 0);
        if (type == 0) {
            itemSwitch.setChecked(false);
        } else {
            itemSwitch.setChecked(true);
        }
        ClickUtil.click(itemSwitch, () -> {
            if (itemSwitch.isChecked()) {
                mPresenter.setType(0);
//                itemSwitch.setChecked(false);
            } else {
                mPresenter.setType(1);
//                itemSwitch.setChecked(true);
            }
        });
        ClickUtil.click(languageChoose, () -> {
            TipUtil.getTip(this, App.INSTANCE.getString(R.string.strGoogleSettingActivity202), new TipUtil.OnTipSure() {
                @Override
                public void onSure() {
                    if ("en".equals(MyAppLication.getLANGUAGE())) {
                        SPUtils.put(GoogleSettingActivity.this, Constants.LANGUAGE, "zh", "chinese");
                        languageChoose.setText(App.INSTANCE.getString(R.string.strGoogleSettingActivity205));
                        setLocale(Locale.SIMPLIFIED_CHINESE);
                    } else {
                        SPUtils.put(GoogleSettingActivity.this, Constants.LANGUAGE, "en", "english");
                        languageChoose.setText(App.INSTANCE.getString(R.string.strGoogleSettingActivity207));
                        setLocale(Locale.ENGLISH);
                    }
                    Intent intent = new Intent(GoogleSettingActivity.this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);
                }
            }).show();
        });
        // 指纹开启关闭逻辑
        boolean isCheck = (boolean) SPUtils.get(MyAppLication.getApplication(), MyAppLication.getUId(), false, "");
//        mineSettingZw.setEnabled(false);
        mineSettingZw.setChecked(isCheck);
        ClickUtil.click(mineSettingZw, () -> {
            boolean aBoolean = (boolean) SPUtils.get(MyAppLication.getApplication(), MyAppLication.getUId(), false, "");
            if (aBoolean) {
                mineSettingZw.setChecked(false);
                SPUtils.put(MyAppLication.getApplication(), MyAppLication.getUId(), false, "");
            } else {
                if (startFingerprintRecognition()) {
                    //                    ToastUtil.showTextToastById(R.string.prompt_open_fingerprint_hint);
                    SPUtils.put(MyAppLication.getApplication(), MyAppLication.getUId(), true, "");
                    mineSettingZw.setChecked(true);
                    //                    initFingerprintCore();
                }
            }
        });
    }

    public void updateUiSuccess(int type) {
        itemSwitch.setChecked(type == 0 ? false : true);
        SPUtils.put(MyAppLication.getApplication(), Constants.GOOGLETYPE, type);
    }

    @Override
    protected void initView() {
        String verName = APKVersionCodeUtils.getVerName(mActivity);
        setText(version, verName);
        ClickUtil.click(versionLayout, () -> {
            mPresenter.getVersion();
        });
        toolBarLayout.setLeftButtonOnClickLinster(v -> finish());
    }


    public void onVersionDataSuccess(AppVersionBean versionBean) {
        if (versionBean == null) {
            return;
        }
        String verName = APKVersionCodeUtils.getVerName(mActivity);
        versionBean.setViewStyle(BaseVersion.NOTIFYCATION_STYLE);
        if (APKVersionCodeUtils.compareVersion(versionBean.getVersion(), verName) == 1) {
            FragmentTransaction fragmentTransaction = mActivity.getFragmentManager().beginTransaction();
            getFragmentManager().beginTransaction();
            AppUpDateFragment appUpDateFragment = new AppUpDateFragment();//自定义UI
            Bundle bundle = new Bundle();
            bundle.putSerializable(BaseUpdateDialogFragment.INTENT_KEY, versionBean);
            appUpDateFragment.setArguments(bundle);
            appUpDateFragment.show(fragmentTransaction, "update");
        } else {
            ToastUtil.showShort("当前已是最新版本");
        }
    }

    public void setLocale(Locale myLocale) {
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
        LanguageUtil.saveLanguageSetting(this, myLocale);
    }


    /**
     * 开始指纹识别
     */
    private boolean startFingerprintRecognition() {
        if (mFingerprintCore.isSupport()) {
            if (!mFingerprintCore.isHasEnrolledFingerprints()) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.fingerprint_recognition_not_enrolled));
                FingerprintUtil.openFingerPrintSettingPage(this);
                return false;
            }
            return true;
        } else {
            ToastUtil.showShort(App.INSTANCE.getString(R.string.fingerprint_recognition_not_support));
            return false;
        }
    }


}
